<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="订单编号" prop="sgoNumber">
        <el-input
          v-model="queryParams.sgoNumber"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item label="商品名称" prop="productName">
        <el-input
          v-model="queryParams.productName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="价格" prop="productPrice">-->
<!--        <el-input-->
<!--          v-model="queryParams.productPrice"-->
<!--          placeholder="请输入${comment}"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->





      <el-form-item label="用户姓名" prop="userName">
        <el-input
          v-model="queryParams.userName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="userPhone">
        <el-input
          v-model="queryParams.userPhone"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地址" prop="addressName">
        <el-input
          v-model="queryParams.addressName"
          placeholder="请输入"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>


      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:orders:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['manage:orders:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="ordersList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="订单id" align="center" prop="sgoId" />
      <el-table-column label="订单编号" align="center" prop="sgoNumber" />
<!--      <el-table-column label="${comment}" align="center" prop="usersId" />-->
<!--      <el-table-column label="${comment}" align="center" prop="spsId" />-->
      <el-table-column label="商品名称" align="center" prop="productName" />
      <el-table-column label="价格" align="center" prop="productPrice" />
      <el-table-column label="数量" align="center" prop="productNum" />
<!--      <el-table-column label="${comment}" align="center" prop="spNameUrl" />-->
<!--      <el-table-column label="${comment}" align="center" prop="size" />-->
<!--      <el-table-column label="${comment}" align="center" prop="season" />-->
<!--      <el-table-column label="${comment}" align="center" prop="applicableGender" />-->
<!--      <el-table-column label="${comment}" align="center" prop="specificationFive" />-->
      <el-table-column label="订单状态">
        <template slot-scope="scope">
          <span v-if="scope.row.status==1">
              <el-tag type=''  effect="dark">
                        订单未完成
              </el-tag>
          </span>

          <span v-if="scope.row.status==2">
              <el-tag type='success'  effect="dark">
                        订单已完成
              </el-tag>
          </span>
          <span v-if="scope.row.status==3">
              <el-tag type='info'  effect="dark">
                        订单退款中
              </el-tag>
          </span>

          <span v-if="scope.row.status==4">
              <el-tag type='danger'  effect="dark">
                        订单已退款
              </el-tag>
          </span>

        </template>
      </el-table-column>




      <el-table-column label="用户昵称" align="center" prop="userName" />
      <el-table-column label="电话" align="center" prop="userPhone" />
      <el-table-column label="地址" align="center" prop="addressName" />
<!--      <el-table-column label="${comment}" align="center" prop="isdelete" />-->
<!--      <el-table-column label="${comment}" align="center" prop="total" />-->
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['manage:orders:edit']"
          >修改</el-button>
<!--          <el-button-->
<!--            size="mini"-->
<!--            type="text"-->
<!--            icon="el-icon-delete"-->
<!--            @click="handleDelete(scope.row)"-->
<!--            v-hasPermi="['manage:orders:remove']"-->
<!--          >删除</el-button>-->


          <span v-if="scope.row.status==3">
               <el-button
                 size="mini"
                 type="text"
                 icon="el-icon-edit"
                 @click="sp(scope.row)"
                 v-hasPermi="['manage:orders:sp']"
               >审批</el-button>
          </span>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />
    <!-- 审批对话 -->



    <!-- 添加或修改订单管理对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">



<!--        <el-form-item label="${comment}" prop="size">-->
<!--          <el-input v-model="form.size" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="season">-->
<!--          <el-input v-model="form.season" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="applicableGender">-->
<!--          <el-input v-model="form.applicableGender" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="specificationFive">-->
<!--          <el-input v-model="form.specificationFive" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
        <el-form-item label="用户名称" prop="userName">
          <el-input v-model="form.userName" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="电话" prop="userPhone">
          <el-input v-model="form.userPhone" placeholder="请输入" />
        </el-form-item>
        <el-form-item label="地址" prop="addressName">
          <el-input v-model="form.addressName" placeholder="请输入" />
        </el-form-item>
<!--        <el-form-item label="${comment}" prop="isdelete">-->
<!--          <el-input v-model="form.isdelete" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
<!--        <el-form-item label="${comment}" prop="total">-->
<!--          <el-input v-model="form.total" placeholder="请输入${comment}" />-->
<!--        </el-form-item>-->
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listOrders, getOrders, delOrders, addOrders, updateOrders ,refund,refundf} from "@/api/manage/orders";

export default {
  name: "Orders",
  data() {
    return {

      items: [//状态
        { type: '', label: '订单未完成' },
        { type: 'success', label: '订单已完成' },
        { type: 'info', label: '订单退款中' },
        { type: 'danger', label: '订单已退款' },
      ],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 订单管理表格数据
      ordersList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        sgoNumber: null,
        usersId: null,
        spsId: null,
        productName: null,
        productPrice: null,
        productNum: null,
        spNameUrl: null,
        size: null,
        season: null,
        applicableGender: null,
        specificationFive: null,
        status: null,
        userName: null,
        userPhone: null,
        addressName: null,
        isdelete: null,
        total: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
  },
  methods: {
    sp(row){
      this.$confirm('确认退款, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '拒绝',
        type: 'warning'
      }).then(() => {
        refund(row.sgoId).then(()=>{
          this.$message({
            type: 'success',
            message: '退款成功!'
          });
          this.getList();
        })

       }).catch(() => {
        refundf(row.sgoId).then(()=>{
          this.$message({
            type: 'info',
            message: '已拒绝退款'
          });
          this.getList();
        })

      });


    },
    /** 查询订单管理列表 */
    getList() {
      this.loading = true;
      listOrders(this.queryParams).then(response => {
        this.ordersList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        sgoId: null,
        sgoNumber: null,
        usersId: null,
        spsId: null,
        productName: null,
        productPrice: null,
        productNum: null,
        spNameUrl: null,
        size: null,
        season: null,
        applicableGender: null,
        specificationFive: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        status: null,
        userName: null,
        userPhone: null,
        addressName: null,
        isdelete: null,
        total: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.sgoId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加订单管理";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const sgoId = row.sgoId || this.ids
      getOrders(sgoId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改订单管理";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.sgoId != null) {
            updateOrders(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addOrders(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const sgoIds = row.sgoId || this.ids;
      this.$modal.confirm('是否确认删除订单管理编号为"' + sgoIds + '"的数据项？').then(function() {
        return delOrders(sgoIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('manage/orders/export', {
        ...this.queryParams
      }, `orders_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
